﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using ScrumTable.Common;
using ScrumTable.Config.ConfigManagement;
using ScrumTable.Config.ElementManagement;
using ScrumTable.UI.ViewModel.Controller;
using Microsoft.Practices.Unity;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel
{
    /// <summary>
    /// Base Class for all ViewModel Classes
    /// </summary>
    public class ViewModelBase : DisposeBase, INotifyPropertyChanged
    {
        
        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private NavigationTarget _currentTarget = NavigationTarget.Null;
        private ScrumController _naviController;
        private static ScrumViewModelData _scrumData;
        private static ElementManager _elementManager;
        private static IUnityContainer _appContainer;

        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets or sets the navigationController
        /// </summary>
        /// <value>navigationController</value>
        [Dependency]
        public ScrumController NaviController
        {
            get
            {
                return _naviController;
            }
            set
            {
                _naviController = value;
                _naviController.ChangedView += OnChangedView;
            }
        }


        /// <summary>
        /// Gets the Elementmanager
        /// </summary>
        public ElementManager ElementManager
        {
            get
            {
                if(_elementManager==null)
                {
                    _elementManager = ((IAppScreen) Application.Current.MainWindow).Container.Resolve<ElementManager>();
                }
                return _elementManager;
            }
        }


        /// <summary>
        /// Gets the Elementmanager
        /// </summary>
        protected static ScrumViewModelData ScrumDataGlobal
        {
            get
            {
                if (_scrumData == null)
                {
                    _scrumData = ((IAppScreen)Application.Current.MainWindow).Container.Resolve<ScrumViewModelData>();
                }
                return _scrumData;
            }
        }


        /// <summary>
        /// Gets the Elementmanager
        /// </summary>
        protected static IUnityContainer AppContainer
        {
            get
            {
                if (_appContainer == null)
                {
                    _appContainer = ((IAppScreen) Application.Current.MainWindow).Container;
                }
                return _appContainer;
            }
        }



        /// <summary>
        /// Gets or sets the NavigationTarget target.
        /// If a new value is set then the navigationcontroller moves to the target 
        /// </summary>
        /// <value>The current target.</value>
        public NavigationTarget CurrentTarget
        {
            get
            {
                if( _currentTarget == NavigationTarget.Null)
                {
                    
                    _currentTarget = NaviController.CurrentTarget;
                }
                return _currentTarget;
            }
            set
            {
                NaviController.GoTo(value);
            }
        }

         
        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------

        #endregion

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Sends the property changed.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void SendPropertyChanged( string propertyName)
        {
            if( PropertyChanged != null)
            {
                PropertyChanged( this, new PropertyChangedEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Releases managed (c# or .net) resources.
        /// </summary>
        protected override void ReleaseManagedResources()
        {
            if(_naviController!=null)
                _naviController.ChangedView -= OnChangedView;
            base.ReleaseManagedResources();
        }
        
        #endregion

        #region Events
        //--------------------------------------------------------------------
        // Events
        //--------------------------------------------------------------------
        
        private void OnChangedView(object sender, NavigationEventArgs e)
        {
            _currentTarget = e.Target;
            SendPropertyChanged("CurrentTarget");
        }

        #endregion

        #region INotifyPropertyChanged Members

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}